package com.linkoog.devtools.code_generator.wizard.generate.database.template.panel;

import com.linkoog.devtools.code_generator.template.model.node.TemplateNodeData;
import com.linkoog.devtools.code_generator.setting.configurable.ui.template.TemplateTreeCellRenderer;
import com.linkoog.devtools.code_generator.setting.persistent.CodeGenDevToolsPersistent;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.JBUI.Borders;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.function.Consumer;

public class LeftPanelBuilder {

    private JPanel myMainPanel;
    private JScrollPane scrollPane;
    private JTree templateTree;


    private Consumer<TemplateNodeData> chickConsumer;


    public LeftPanelBuilder() {
        myMainPanel.setBorder(new LineBorder(Color.LIGHT_GRAY));
        myMainPanel.setMinimumSize(JBUI.size(150, -1));
        myMainPanel.setPreferredSize(JBUI.size(150, -1));

        scrollPane.setBorder(Borders.empty());
        scrollPane.setViewportBorder(Borders.empty());

        //final JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(templateTree, true);
        //myMainPanel.add(scrollPane, BorderLayout.CENTER);

        initTemplateTree();
    }


    private void initTemplateTree() {
        templateTree.putClientProperty("JTree.lineStyle", "Horizontal");
        templateTree.setRootVisible(false);
        templateTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        templateTree.setCellRenderer(new TemplateTreeCellRenderer());
        templateTree.setEditable(false);

        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
        DefaultTreeModel model = new DefaultTreeModel(rootNode);


        List<TemplateNodeData> templateNodeDataList = CodeGenDevToolsPersistent.getInstance().getTemplateDataList();
        for (TemplateNodeData templateNodeData : templateNodeDataList) {
            rootNode.add(new DefaultMutableTreeNode(templateNodeData));
        }

        templateTree.setModel(model);


        templateTree.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = templateTree.getRowForLocation(e.getX(), e.getY());
                if (row == -1) {
                    /*templateTree.setSelectionRow(row);
                    if (chickConsumer != null){
                        chickConsumer.accept(null);
                    }*/
                    return;
                }

                templateTree.setSelectionRow(row);
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) templateTree.getLastSelectedPathComponent();

                Object selectedNodeObject = selectedNode.getUserObject();
                if (selectedNodeObject instanceof TemplateNodeData) {
                    TemplateNodeData templateNodeData = (TemplateNodeData) selectedNodeObject;
                    if (chickConsumer != null) {
                        chickConsumer.accept(templateNodeData);
                    }
                }
            }
        });

    }

    public Consumer<TemplateNodeData> getChickConsumer() {
        return chickConsumer;
    }

    public void setChickConsumer(Consumer<TemplateNodeData> chickConsumer) {
        this.chickConsumer = chickConsumer;
    }

    public JPanel getMyMainPanel() {
        return myMainPanel;
    }

    public JTree getTemplateTree() {
        return templateTree;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new BorderLayout(0, 0));
        scrollPane = new JScrollPane();
        myMainPanel.add(scrollPane, BorderLayout.CENTER);
        templateTree = new JTree();
        scrollPane.setViewportView(templateTree);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }
}
